define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        //{field: 'group.name', title: __('Group')},
                        {field: 'username', title: '会员账号', operate: 'LIKE'},
                        {field: 'puser.username', title: '所属上级',operate:false},
                        {field: 'wallet.money', title:'余额', operate: false},
                        {field: 'wallet.name', title:'取款姓名', operate: 'LIKE'},
                        {field: 'logintime', title: __('Logintime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {field: 'loginip', title: __('Loginip'), formatter: Table.api.formatter.search},
                        {field: 'jointime', title: __('Jointime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        //{field: 'joinip', title: __('Joinip'), formatter: Table.api.formatter.search},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden'),frozen:'冻结'}},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table, events:
                            Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons:[
                                {
                                    name: 'agent',
                                    title: __('更改代理线'),
                                    classname: 'btn btn-xs btn-success update-agent',
                                    text:'更改代理线',
                                },
                            ]
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
            $(document).on("click", ".update-agent", function () { //
                var index = $(this).parent().parent().attr('data-index');
                var data = Table.api.getrowdata(table, index);
                var url = 'user/user/editagent/ids/' + data.id;

                layer.open({
                    title:'修改代理账号',
                    type: 1,
                    skin: 'layui-layer-rim', //加上边框
                    area: ['420px', '240px'], //宽高
                    content: $('#update-agent'),
                    btn:['确认','取消'],
                    yes: function(index, layero){
                        var username = $("input[name='pusername']").val();
                        $.post(
                            url,
                            {username:username},
                            function (data) {
                                if( data.code == 0 ){
                                    Toastr.error(data.msg);
                                }else{
                                    Toastr.success(data.msg);
                                    $("input[name='pusername']").val('');
                                    table.bootstrapTable('refresh');
                                    Layer.close(index);
                                }
                            }
                        );
                    }

                });

            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});